<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Admin\Controller;

/**
 * 员工信息
 */
class UserController extends HomeController {

    public function _initialize(){
        parent::_initialize();
        $this->is_login();      //判断是否登录
    }

    //员工列表
	public function get_list(){
        $requestdata = file_get_contents("php://input");
        $paramData = json_decode($requestdata, true);
        $model = D("User");
        $field = "a.*,a.id as user_id,DATE_FORMAT(FROM_UNIXTIME(a.create_time), '%Y-%m-%d %H:%i') as create_date";
        $result = $model->get_user_list($paramData,$field);
        foreach($result['list'] as $key=>$val){
            unset($val['password']);
            $result['list'][$key] = $val;
        }
        $returnData = [
            'code' => 200,
            'data' => $result['list']?$result['list']:[],
            'total' => $result['count']
        ];
        echo json_encode($returnData);
    }


    //添加/修改员工
    public function save(){
        $requestdata = file_get_contents("php://input");
        $paramData = json_decode($requestdata, true);
        $model = D("User");
        if(empty($paramData['user_name'])) $this->throw_exception('员工姓名不能为空');
        if(empty($paramData['login_name'])) $this->throw_exception('登录账号不能为空');
        if(empty($paramData['user_id']) && empty($paramData['password'])) $this->throw_exception('密码不能为空');
        if($paramData['password'] && strlen($paramData['password']) < 6) $this->throw_exception('密码长度最少6位');
        $paramData['userId'] = $this->user_info['id'];
        $result = $model->user_save($paramData);
        $this->throw_exception($result['msg'],$result['code']);

    }

}